package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.BuffConfig;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.model.Buff;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.skill.ConfigService;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.BuffReport;
import com.sghd.pojo.fight.game.ChessType;
import com.sghd.pojo.fight.game.SkillReport;
import com.sghd.pojo.fight.game.TargetReport;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author frank
 * @date 2018-11-21 15:46
 * @desc
 **/
@Component
public class LMshieldEffect implements Effect {
    @Autowired
    private ConfigService configService;
    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        //雷魔法盾buffId
        int buffId = MapUtils.getInteger(ctx, Effect.BUFF_ID);
        BuffConfig buffConfig = configService.getBuff(buffId);

        ChessType type = ChessType.valueOf(ctx.get(Effect.MAGIC_TYPE).toString());
        ctx.put(MAGIC_VALUE, ((Player) target).getMagic(type));
        int level = owner.getSkillLevel(config.getId());
        Map<String, Object> limitM = MapUtils.getMap(ctx, String.valueOf(level));
        int limit = MapUtils.getIntValue(limitM, Effect.MSHIE_LIMIT);
        if (owner.getMagic(type) <= limit) {//魔法值小于13点不消耗回合
            owner.setRound(owner.getRound() + config.getRound());//加一个回合(默认已经消耗一次机会)
            skillReport.setRound(0);
        }
        Buff buff = getBuff(config, buffConfig, ctx, owner, target);
        context.addBuff(target, buff);
        targetReport.addBuff(BuffReport.valueOf(buffId, buff.getType(), buff.getValue() + buff.getType().getValue(), buff.getTime(), buff.getLevel()));
    }


    @Override
    public EffectType getType() {
        return EffectType.SK_L_MSHIELD;
    }
}
